﻿namespace OasisCiq.Xml
{
    using System.Xml;

    public static class XmlUtilities
    {
        public static XmlReader Wrap(XmlReader reader)
        {
            var settings = reader.Settings == null
                 ? new XmlReaderSettings()
                 : reader.Settings.Clone();
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace = true;
            var wrapper = XmlReader.Create(reader, settings);
            wrapper.Read(); // from initial to interactive state
            return wrapper;
        }

        public static bool MoveToElement(XmlReader reader, XmlQualifiedName elementName)
        {
            while (true)
            {
                if (reader.NodeType == XmlNodeType.Element &&
                    reader.NamespaceURI == elementName.Namespace && 
                    reader.LocalName == elementName.Name)
                {
                    return true;
                }

                if (!reader.Read())
                {
                    return false;
                }
            }
        }
    }
}
